package xiangmu.schoolpaper.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xiangmu.schoolpaper.mapper.PersonalCenterMapper;
import xiangmu.schoolpaper.mapper.UserMapper;
import xiangmu.schoolpaper.pojo.DTO.UpdatePasswordDTO;
import xiangmu.schoolpaper.pojo.VO.PersonalCenterVO;
import xiangmu.schoolpaper.pojo.entity.User;
import xiangmu.schoolpaper.service.PersonalCenterService;

@Service
public class PersonalCenterServiceImpl implements PersonalCenterService {
    @Autowired
    private PersonalCenterMapper personalCenterMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PersonalCenterVO getPersonalCenterById(String account){
        return personalCenterMapper.getPersonalCenterById(account);
    }

    @Override
    public boolean updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        // 1. 根据 account 查用户
        User user = userMapper.getUserByAccount(updatePasswordDTO.getAccount());
//        if (user == null) {
//            throw new RuntimeException("账号不存在");
//        }

        // 2. 校验原密码
        if (!user.getPassword().equals(updatePasswordDTO.getOldPassword())) {
            System.out.println("原密码错误");
            return false; // 原密码错误
        }

        // 3. 更新新密码
        int rows = userMapper.updatePassword(updatePasswordDTO.getAccount(), updatePasswordDTO.getNewPassword());
        if (rows > 0) {
            System.out.println("修改成功");
        }
        return rows > 0;
    }

}
